﻿$(function() {

    // Global menu variables
    var objMenu = $('.floating-menu'),
        objMenuLink = $('.floating-menu a'),
        $menu = $('.open-menu'),
        $openDropdown = $('.open-dropdown'),
        $close = $('.close-menu');

    // Mobile menu open/close
    $menu.on('click', function() {
        objMenu.addClass('expanded');
    });

    // Floating menu hyperlink
    if ($('nav').hasClass('navbar-single-page')) {
        objMenuLink.on('click', function() {
            objMenu.removeClass('expanded');
        });
    }

    // Open dropdown/megamenu
    $openDropdown.on('click', function(e) {

        e.preventDefault();

        var liParent = $(this).parent().parent(),
            liDropdown = liParent.find('.navbar-dropdown');

        liParent.toggleClass('expanded');

        if (liParent.hasClass('expanded')) {
            liDropdown.slideDown();
        } else {
            liDropdown.slideUp();
        }
    });

    // Close menu (mobile)
    $close.on('click', function() {
        $('nav').find('.expanded').removeClass('expanded');
        $('nav').find('.navbar-dropdown').slideUp();
    });

    // Global functions
    function toggleOpen(el) {
        $(el).toggleClass('open');
    }


    // Sticky header ----------------------------------------------------------------
    var navbarFixed = $('nav.navbar-fixed');

    // 重新加载页面时-检查页面是否偏移
    if ($(document).scrollTop() > 94) {
        navbarFixed.addClass('navbar-sticked');
    }
    // 添加滚动的粘性菜单
    $(document).on('bind ready scroll', function() {
        var docScroll = $(document).scrollTop();
        if (docScroll >= 10) {
            navbarFixed.addClass('navbar-sticked');
        } else {
            navbarFixed.removeClass('navbar-sticked');
        }
    });


    // 前端滑块 ----------------------------------------------------------------
    var arrowIcons = [
        '<span class="icon icon-chevron-left"></span>',
        '<span class="icon icon-chevron-right"></span>'
    ];

    $.each($(".owl-slider"), function(i, n) {

        $(n).owlCarousel({
            autoHeight: false,
            navigation: true,
            navigationText: arrowIcons,
            items: 1,
            singleItem: true,
            addClassActive: true,
            transitionStyle: "fadeUp",
            afterMove: animatetCaptions,
            autoPlay: 8000,
            stopOnHover: false
        });

        animatetCaptions();

        function animatetCaptions(event) {
            "use strict";
            var activeItem = $(n).find('.owl-item.active'),
                timeDelay = 100;
            $.each(activeItem.find('.animated'), function(j, m) {
                var item = $(m);
                item.css('animation-delay', timeDelay + 'ms');
                timeDelay = timeDelay + 180;
                item.addClass(item.data('animation'));
                setTimeout(function() {
                    item.removeClass(item.data('animation'));
                }, 2000);
            });
        }

        if ($(n).hasClass('owl-slider-fullscreen')) {
            $('.header-content .item').height($(window).height());
        }
    });

    // 滚动到顶部 ----------------------------------------------------------------
    var $wrapper = $('.wrapper');
    $wrapper.append($("<div class='scroll-top'><i class='icon icon-chevron-up'></i></div>"));

    var $scrollbtn = $('.scroll-top');

    $(document).on('ready scroll', function() {
        var docScrollTop = $(document).scrollTop(),
            docScrollBottom = $(window).scrollTop() + $(window).height() == $(document).height();

        if (docScrollTop >= 150) {
            $scrollbtn.addClass('visible');
        } else {
            $scrollbtn.removeClass('visible');
        }
        if (docScrollBottom) {
            $scrollbtn.addClass('active');
        } else {
            $scrollbtn.removeClass('active');
        }
    });

    $scrollbtn.on('click', function() {
        $('html,body').animate({
            scrollTop: $('body').offset().top
        }, 1000);
        return false;
    });


    // Check if some filter boxes has class active
    // then show hidden filters
    $('.filters .filter-box').each(function() {
        if ($(this).hasClass('active')) {
            $(this).find('.filter-content').show();
        }
    });

    var $filtersTitle = $('.filters .title');

    // Add emtpy span on title
    $filtersTitle.append('<span>' + '</span>');

    // Toggle filter function
    $filtersTitle.on('click', function(e) {
        var $this = $(this),
            $parent = $this.parent();
        $parent.toggleClass('active');

        if ($parent.hasClass('active')) {
            $parent.find('.filter-content').slideDown(300);
        } else {
            $parent.find('.filter-content').slideUp(200);
        }
    });

    // Update filter results - close dropdown filters -------------------------------------------
    $('.filters .filter-update').on('click', function(e) {
        $(this).closest('.filter-box')
            .removeClass('active')
            .find('.filter-content').slideUp(200);
    });


    // Only for filters topbar ---------------------------------------------------------
    $('.filters input').on('change', function() {
        if ($(this).is(':checked')) {
            var $labelText = $(this).parent().find('label').text(),
                $title = $(this).closest('.filter-box').find('.title');

            $title.find('span').text($labelText);
        }
    });

    // Show hide filters (only for mobile) ----------------------------------------------------
    $('.toggle-filters-mobile').on('click', function() {
        $('.filters').addClass('active');
    });
    $('.toggle-filters-close').on('click', function() {
        $('.filters').removeClass('active');
        $('html,body').animate({
            scrollTop: $('body').offset().top
        }, 800);
        return false;
    });

    // Single page navigation (scroll to) -------------------------------------------------
    if ($('nav').hasClass('navbar-single-page')) {

        var $singleHyperlink = $('.navigation-main a');

        $singleHyperlink.on('click', function() {

            $singleHyperlink.removeClass('current');

            $(this).addClass('current');

            $('html, body').animate({
                scrollTop: $($(this).attr('href')).offset().top - $('.navigation-main').height()
            }, 500);
            return false;
        });

        // Magnific popup scroll to content ------------------------------------------------------
        $('.mfp-open-scrollto').on('click', function() {
            $('html,body').animate({
                scrollTop: $('.mfp-content').offset().top - 200
            }, 300);
            return false;
        });
    }

});

$(window).bind('load', function() {
    setTimeout(function() {
        $('.page-loader').addClass('loaded');
    }, 1000);
});